
#import <QuartzCore/QuartzCore.h>
#import "FindLampResultViewController.h"
#import "CustomNavigationBar.h"
#import "UINavigationItem+Customization.h"
#import "HelpViewController.h"
#import "Data.h"
#import "lampmeta.h"
#import "FindLampResultWebViewController.h"
#import "ComparePriceViewController.h"
#import "ResultCell.h"
#import "CalculatedInfoWebViewController.h"

@implementation FindLampResultViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIView *v = [[UIView alloc] initWithFrame:self.tableView.frame];
    v.backgroundColor = [UIColor clearColor];
    self.tableView.backgroundView = v;
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];
    
    CustomNavigationBar* customNavigationBar = (CustomNavigationBar*)self.navigationController.navigationBar;
    [customNavigationBar applyDefaultStyle];
    
    UIButton* backButton = [customNavigationBar backButtonWith:[UIImage imageNamed:@"btn-back-off.png"] highlight:[UIImage imageNamed:@"btn-back-on.png"] leftCapWidth:14.0 title:_(@"Välj effekt")];
    backButton.titleLabel.textColor = [UIColor whiteColor];
    backButton.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.title = _(@"Resultat");
    [self.navigationItem customizeTitleInNavigationBarWithHeight:self.navigationController.navigationBar.frame.size.height];
    
    UIButton *infoButton = [[UIButton alloc] initWithFrame:CGRectZero];
    [infoButton setImage:[UIImage imageNamed:@"btn-info-off.png"] forState:UIControlStateNormal];
    [infoButton setImage:[UIImage imageNamed:@"btn-info-on.png"] forState:UIControlStateHighlighted];
    [infoButton setImage:[UIImage imageNamed:@"btn-info-on.png"] forState:UIControlStateSelected];
    [infoButton sizeToFit];
    [infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *infoBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
    infoBarButtonItem.accessibilityLabel = @"Hjälp";
    self.navigationItem.rightBarButtonItem = infoBarButtonItem;
    
    NSString *locale = @"sv";
    NSString *file = [locale stringByAppendingFormat:@"_%@", @"Sa_har_vi_raknat"];
    NSString *html = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] 
                                                         pathForResource:file
                                                         ofType:@"html"] 
                                               encoding:NSUTF8StringEncoding error:nil];
    html = [html stringByReplacingOccurrencesOfString:@"../" withString:@""];
    NSLog(@"%@", file);

    NSString *typeString = self.savingsCalculator.lampItem[kLampItemType];
    self.titleLabel.text = [NSString stringWithFormat:_(@"%@ lm"), [[Data sharedInstance] lumenStringFromWattValue:self.savingsCalculator.effect lampType:typeString]];
    self.titleLabel.accessibilityLabel = [self.titleLabel.text stringByReplacingOccurrencesOfString:@"–" withString:@"till"];
    self.titleLabel.accessibilityLabel = [self.titleLabel.accessibilityLabel stringByReplacingOccurrencesOfString:@"lm" withString:@"lumen"];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.separatorColor = nil;
    self.tableHeaderViewLabel.textColor = [UIColor whiteColor];
    self.tableHeaderViewLabel.layer.shadowColor = [UIColor blackColor].CGColor;
    self.tableHeaderViewLabel.layer.shadowOpacity = 0.1;
    self.tableHeaderViewLabel.layer.shadowRadius = 0;
    self.tableHeaderViewLabel.layer.shadowOffset = CGSizeMake(0.0f, -0.9f);
}

- (void)viewDidUnload
{
    [self setTableView:nil];
    [self setHeaderLabel:nil];
    [self setTitleLabel:nil];
    [self setTableHeaderView:nil];
    [self setTableHeaderViewLabel:nil];
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)showInfo
{
    HelpViewController *helpVC = [[HelpViewController alloc] initWithNibName:nil bundle:nil];
    helpVC.documentFile = _(@"ResultList");
    helpVC.title = _(@"Hjälp");
    [self presentModalViewController:helpVC animated:YES];
}

- (NSString *)energyImageNameForLampWithEnergyClass:(NSString *)energyClass;
{
    NSString *energyImageName = nil;
    if ([energyClass isEqualToString:@"A++-A"]) {
        energyImageName = @"ic-energi-aplusplus-a.png";
    } else if ([energyClass isEqualToString:@"A+-A"]) {
        energyImageName = @"ic-energi-aplus-a.png";
    } else if ([energyClass isEqualToString:@"B"]) {
        energyImageName = @"ic-energi-b";
    } else if ([energyClass isEqualToString:@"C"]) {
        energyImageName = @"ic-energi-c";
    } else if ([energyClass isEqualToString:@"C-D"]) {
        energyImageName = @"ic-energi-c-d";
    }
    return energyImageName;
}

- (NSString *)energyMarkNameForLampWithEnergyClass:(NSString *)energyClass
{
    NSString *energyImageName = nil;
    if ([energyClass isEqualToString:@"A++-A"]) {
        energyImageName = @"Energiklass A++ till A";
    } else if ([energyClass isEqualToString:@"A+-A"]) {
        energyImageName = @"Energiklass A+ till A";
    } else if ([energyClass isEqualToString:@"B"]) {
        energyImageName = @"Energiklass B";
    } else if ([energyClass isEqualToString:@"C"]) {
        energyImageName = @"Energiklass C";
    } else if ([energyClass isEqualToString:@"C-D"]) {
        energyImageName = @"Energiklass C till D";
    }
    return energyImageName;
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 117.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.tableHeaderView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.savingsCalculator.substitutes.count;
}

#define kResultCellNibName @"ResultCell"
#define kNibName @"ButtonCell"
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ResultCell *cell = (ResultCell *)[tableView dequeueReusableCellWithIdentifier:kResultCellNibName];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:kResultCellNibName owner:self options:nil][0];
        cell.delegate = self;
        [cell initCell];
    }
    cell.tag = indexPath.row;
    NSDictionary *item = self.savingsCalculator.substitutes[indexPath.row];
    cell.lampLabel.text = item[@"name"];
    cell.lampPriceLabel.text = [NSString stringWithFormat:@"Lampans inköpspris: ca %.0f kr", [self.savingsCalculator purchasePriceForSubstituteAtIndex:indexPath.row]];
    cell.lampPriceLabel.accessibilityLabel = [cell.lampPriceLabel.text stringByReplacingOccurrencesOfString:@"kr" withString:@"kronor"];
    cell.energyMark.image = [UIImage imageNamed:[self energyImageNameForLampWithEnergyClass:item[@"energy_class"]]];
    cell.energyMark.accessibilityLabel = [self energyMarkNameForLampWithEnergyClass:item[@"energy_class"]];
    cell.savingsLabel.text = [NSString stringWithFormat:@"ca %.0f kr",[self.savingsCalculator savingForSubstituteAtIndex:indexPath.row]];
    cell.savingsLabel.accessibilityLabel = [cell.savingsLabel.text stringByReplacingOccurrencesOfString:@"kr" withString:@"kronor"];
    cell.yearLabel.hidden = [self.savingsCalculator savingForSubstituteAtIndex:indexPath.row] <= 0;
    cell.yearLabel.text = [NSString stringWithFormat:@"på %@ år", item[@"years"]];
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < self.savingsCalculator.substitutes.count) {
        NSDictionary *item = self.savingsCalculator.substitutes[indexPath.row];
        FindLampResultWebViewController *findLampResultWebVC = [[FindLampResultWebViewController alloc] initWithDefaultNibAndDocumentName:item[@"document"]];
        findLampResultWebVC.backButtonTitle = _(@"Tillbaka");
        findLampResultWebVC.headerViewLabel = item[@"name"];
        findLampResultWebVC.title = _(@"Resultat");
        findLampResultWebVC.subTitle = self.titleLabel.text;
        [self.navigationController pushViewController:findLampResultWebVC animated:YES];
    }
}

#pragma mark - Button cell delegate
- (void)buttonCellButtonPressed
{
    ComparePriceViewController *compVC = [[ComparePriceViewController alloc] initWithNibName:nil bundle:nil calculator:self.savingsCalculator];
    [self presentModalViewController:compVC animated:YES];
}

#pragma mark - Result cell
- (void)changeLampPriceForResultCellAtIndex:(NSInteger)index
{
    ComparePriceViewController *compVC = [[ComparePriceViewController alloc] initWithNibName:nil bundle:nil calculator:self.savingsCalculator];
    compVC.subtituteIndex = index;
    compVC.lampString = self.savingsCalculator.substitutes[index][@"name"];
    [self presentModalViewController:compVC animated:YES];
}

- (void)showInformationForResultCell
{
    HelpViewController *controller = [[HelpViewController alloc] initWithNibName:nil bundle:nil];
    controller.documentFile = @"Sa_har_vi_raknat";
    controller.title = _(@"Så har vi räknat");
    
    [self presentModalViewController:controller animated:YES];
}
@end
